package com.book.lizhi.ui;

import android.app.Application;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.waps.AppConnect;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.book.lizhi.AppApplication;
import com.book.lizhi.BaseActivity;
import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.model.FilmModel;
import com.book.lizhi.util.AppLog;
import com.book.lizhi.widget.HttpAsyncLayout;
import com.book.lizhi.widget.HttpAsyncLayout.OnHttpAsyncListner;

public class FilmDetailActivity extends BaseActivity {

	private HttpAsyncLayout mAsyncLayout;
	private TextView mContentView;
	private TextView mSourceView;
	private FilmModel mArticleModel;

	private View mShareView;
	private View mFavView;
	private TextView mShowFavView;
	
	private NetworkImageView mImageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_article);

		Toolbar toolbar = (Toolbar) findViewById(R.id.toobar);
		setSupportActionBar(toolbar);
		mArticleModel = (FilmModel) getIntent().getSerializableExtra(Constants.ARTICLE);

		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setTitle(mArticleModel.name);

		mAsyncLayout = (HttpAsyncLayout) findViewById(R.id.http_async_layout);

		mContentView = (TextView) findViewById(R.id.article_content);
		Typeface typeface = Typeface.createFromAsset(getAssets(), "xin_hei.otf");
		mContentView.setTypeface(typeface);
		mContentView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);

		mContentView.setText(mArticleModel.name + "\n\n" + mArticleModel.brief + "\n\n" + mArticleModel.review);

		mSourceView = (TextView) findViewById(R.id.artice_source);
		mSourceView.setText("去淘宝购买同名图书");

		mFavView = findViewById(R.id.favorite_container);
		mShowFavView = (TextView) findViewById(R.id.article_fav_view);
		if (mArticleModel.isFav) {
			mShowFavView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.iconfont_good_press, 0, 0, 0);
		} else {
			mShowFavView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.iconfont_good, 0, 0, 0);
		}
		mShareView = findViewById(R.id.share_container);
		
		mImageView=(NetworkImageView)findViewById(R.id.show_image);
		mImageView.setVisibility(View.VISIBLE);
		mImageView.setImageUrl(mArticleModel.img, AppApplication.instance.imageLoader);

		addOnListener();

		// 互动广告调用方式
		LinearLayout layout = (LinearLayout) this.findViewById(R.id.AdLinearLayout);
		AppConnect.getInstance(this).showBannerAd(this, layout);

		// sendGetRequest(HttpUtil.getArticleContentURL(mArticleModel.type, mArticleModel.id), mAsyncLayout);

	}

	private void addOnListener() {
		
		mSourceView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String url = mArticleModel.business;
				if (TextUtils.isEmpty(url)) {
					url = "http://s.click.taobao.com/t?e=m%3D2%26s%3DcDVusM%2BUEBkcQipKwQzePDAVflQIoZepK7Vc7tFgwiFRAdhuF14FMaAI%2FtcMbXUZ5x%2BIUlGKNpW8TUbkMajyHFRdAN7hwPSDmSDfbH0f3AiuRefWAhyyI3H5aVC8wvjy";
				}
				Intent intent=new Intent(getApplicationContext(), WebViewActivity.class);
				intent.putExtra(Constants.SOURCE_URL, url);
				intent.putExtra(Constants.TITLE, "手机淘宝网");
				startActivity(intent);
			}
		});
		mAsyncLayout.setOnHttpAsyncListner(new OnHttpAsyncListner() {

			@Override
			public void onSuccessListener(String url, String response) {
				AppApplication.instance.jsonCache.saveJsonStr(url, response);
				mContentView.setText(JSON.parse(response).toString());
			}

			@Override
			public void onRefreshListener() {
				// sendGetRequest(HttpUtil.getArticleContentURL(mArticleModel.type, mArticleModel.id), mAsyncLayout);
			}

			@Override
			public void onErrorListener(VolleyError error) {
				AppLog.e(error.toString());
			}
		});
		mFavView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mArticleModel.isFav) {
					mShowFavView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.iconfont_good, 0, 0, 0);
					mArticleModel.isFav = false;
					//DataBaseMannager.getInstance().deleteFavById(FilmDetailActivity.this, mArticleModel.id);
					Intent intent = new Intent(Constants.DELETE_FAV);
					intent.putExtra("id", mArticleModel.id);
					LocalBroadcastManager.getInstance(FilmDetailActivity.this).sendBroadcast(intent);
				} else {
					mShowFavView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.iconfont_good_press, 0, 0, 0);
					mArticleModel.isFav = true;
					//DataBaseMannager.getInstance().insertFave(FilmDetailActivity.this, mArticleModel);
					Intent intent = new Intent(Constants.FAV);
					intent.putExtra("id", mArticleModel.id);
					LocalBroadcastManager.getInstance(FilmDetailActivity.this).sendBroadcast(intent);
				}
			}
		});
		mShareView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent sendIntent = new Intent();
				sendIntent.setAction(Intent.ACTION_SEND);
				sendIntent.putExtra(Intent.EXTRA_TEXT, "我在励志应用中看到一篇很有用的文章《" + mArticleModel.name + "》下载励志应用看完整文章。http://www.wandoujia.com/apps/com.book.lizhi");
				sendIntent.setType("text/plain");
				startActivity(Intent.createChooser(sendIntent, "励志文章分享"));
			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
